/*
 * Information.java
 *
 * This program demonstrate the use list, combo box and colorchooser.
 *
 * Copyright (c) 2007 Aptech Software Limited. All Rights Reserved.
 */
package demo.controls;

import java.awt.Color;
import java.awt.event.ItemEvent;
import java.util.Date;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Vincent
 */
public class Information extends javax.swing.JFrame {

    DefaultListModel dlm;

    /**
     * Creates new form Information
     */
    public Information() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }

        getContentPane().setBackground(new Color(212, 208, 200));
        initComponents();

        // init combobox Year
        Date d = new Date();
        int year = d.getYear() + 1900;

        for (int i = year; i > year - 23; i--) {
            cboYear.addItem(i);
        }

        // init institue
        String[] instutes = {"Aptech", "Hoa Sen", "Ly Tu Trong"};
        for (String i : instutes) {
            cboInstitute.addItem(i);
        }

        // init list experience
        String[] experience = {"Fresher", "0 - 1 year", "1 - 2 years", "2 - 4 years", "4 - 8 years", "8 - 15 years", "15 years and above"};

        dlm = new DefaultListModel();

        for (String i : experience) {
            dlm.addElement(i);
        }

        lstExperience.setModel(dlm);

        ListSelectionModel selectionModel =
                lstExperience.getSelectionModel();
        selectionModel.setSelectionInterval(0, 3);
        lstExperience.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        lstExperience.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    System.out.println("First = " + e.getFirstIndex() + ", Last = " + e.getLastIndex());
                }
            }
        });
//        lstExperience.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//        lstExperience.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    }

    public boolean valide() {
        if (lstExperience.getSelectedIndex() == 2
                && cboYear.getSelectedIndex() < 2) {
            JOptionPane.showMessageDialog(null,
                    "The year of passing " + cboYear.getSelectedItem()
                    + " is not valid for " + lstExperience.getSelectedValue()
                    + " of experience", "Message",
                    JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
        if (lstExperience.getSelectedIndex() == 3
                && cboYear.getSelectedIndex() < 3) {
            JOptionPane.showMessageDialog(null,
                    "The year of passing " + cboYear.getSelectedItem()
                    + " is not valid for " + lstExperience.getSelectedValue()
                    + " of experience", "Message",
                    JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
        return true;
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblName = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        lblExperience = new javax.swing.JLabel();
        scpExperience = new javax.swing.JScrollPane();
        lstExperience = new javax.swing.JList();
        lblYear = new javax.swing.JLabel();
        cboYear = new javax.swing.JComboBox();
        lblInstitute = new javax.swing.JLabel();
        cboInstitute = new javax.swing.JComboBox();
        btnRemove = new javax.swing.JButton();
        Summit = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        mbrMenubar = new javax.swing.JMenuBar();
        mnuColors = new javax.swing.JMenu();
        mniColor = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Information");
        setResizable(false);
        getContentPane().setLayout(new java.awt.FlowLayout());

        lblName.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblName.setText("Name");
        lblName.setPreferredSize(new java.awt.Dimension(80, 20));
        getContentPane().add(lblName);

        txtName.setPreferredSize(new java.awt.Dimension(170, 20));
        getContentPane().add(txtName);

        lblExperience.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblExperience.setText("Experience");
        lblExperience.setOpaque(true);
        lblExperience.setPreferredSize(new java.awt.Dimension(80, 20));
        getContentPane().add(lblExperience);

        lstExperience.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Fresher", "0 - 1 year", "1 - 2 years", "2 - 4 years", "4 - 8 years", "8 - 15 years", "15 years and above" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstExperience.setPreferredSize(new java.awt.Dimension(170, 100));
        scpExperience.setViewportView(lstExperience);

        getContentPane().add(scpExperience);

        lblYear.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblYear.setText("Year of passing");
        lblYear.setPreferredSize(new java.awt.Dimension(80, 20));
        getContentPane().add(lblYear);

        cboYear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Please Select" }));
        cboYear.setPreferredSize(new java.awt.Dimension(170, 23));
        cboYear.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboYearItemStateChanged(evt);
            }
        });
        getContentPane().add(cboYear);

        lblInstitute.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblInstitute.setText("Institute");
        lblInstitute.setPreferredSize(new java.awt.Dimension(80, 20));
        getContentPane().add(lblInstitute);

        cboInstitute.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Please Select" }));
        cboInstitute.setPreferredSize(new java.awt.Dimension(170, 23));
        getContentPane().add(cboInstitute);

        btnRemove.setText("Remove Item");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });
        getContentPane().add(btnRemove);

        Summit.setText("Submit");
        Summit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SummitMouseClicked(evt);
            }
        });
        Summit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SummitActionPerformed(evt);
            }
        });
        getContentPane().add(Summit);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancel);

        mnuColors.setMnemonic('F');
        mnuColors.setText("Colors");

        mniColor.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        mniColor.setText("Edit ForeColor..");
        mniColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniColorActionPerformed(evt);
            }
        });
        mnuColors.add(mniColor);

        mbrMenubar.add(mnuColors);

        setJMenuBar(mbrMenubar);

        setSize(new java.awt.Dimension(316, 305));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void cboYearItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboYearItemStateChanged
// TODO add your handling code here:
        if (evt.getStateChange() == evt.DESELECTED) {
            valide();
        }
    }//GEN-LAST:event_cboYearItemStateChanged

    private void SummitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SummitActionPerformed
        // TODO add your handling code here:
        if (!valide()) {
            return;
        } else {
            String strName = txtName.getText();
            String strExperience = (String) lstExperience.getSelectedValue();
            String strInstitute = (String) cboInstitute.getSelectedItem();
            String strYear = cboYear.getSelectedItem().toString();

            if (strName.trim().length() == 0
                    || strExperience == null
                    || cboInstitute.getSelectedIndex() == 0
                    || cboYear.getSelectedIndex() == 0) {
                JOptionPane.showMessageDialog(
                        null,
                        "Please Complete the details",
                        "Message ",
                        JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null,
                        " Form is submitted with the following details\n Name                 : "
                        + strName + "\n Experience        : "
                        + strExperience + "\n Institute            : "
                        + strInstitute + "\n Year of passing : " + strYear,
                        "Message ", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_SummitActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
// TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void mniColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniColorActionPerformed
// TODO add your handling code here:
        FontColor colorDialog = new FontColor(this, true);
        colorDialog.setVisible(true);
        Color color = colorDialog.getColor();
        txtName.setForeground(color);
        lstExperience.setForeground(color);
        cboInstitute.setForeground(color);
        cboYear.setForeground(color);
    }//GEN-LAST:event_mniColorActionPerformed

    private void SummitMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SummitMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_SummitMouseClicked

    private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
        int[] index = lstExperience.getSelectedIndices();

        for (int i = index.length - 1; i >= 0; i--) {
            dlm.remove(index[i]);
        }
    }//GEN-LAST:event_btnRemoveActionPerformed
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Information().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Summit;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnRemove;
    private javax.swing.JComboBox cboInstitute;
    private javax.swing.JComboBox cboYear;
    private javax.swing.JLabel lblExperience;
    private javax.swing.JLabel lblInstitute;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblYear;
    private javax.swing.JList lstExperience;
    private javax.swing.JMenuBar mbrMenubar;
    private javax.swing.JMenuItem mniColor;
    private javax.swing.JMenu mnuColors;
    private javax.swing.JScrollPane scpExperience;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
}
